package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.controller.lb.ILoadBalance;
import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@Slf4j
@RestController
public class ConsumerController {

    //    public static final String PAYMENT_SERVICE="http://localhost:8001";
    //Ribbon和eureka整合之后，Consumer可以直接调用服务，不用再关心地址和端口号，而且还有负载均衡功能了。(在 RestTemplate的配置中添加LoadBalanced)
    public static final String PAYMENT_SERVICE = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    RestTemplate restTemplate;

    @Resource
    ILoadBalance loadBalance;

    @Resource
    DiscoveryClient discoveryClient;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_SERVICE + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_SERVICE + "/payment/get/" + id, CommonResult.class);
    }


    @GetMapping("/consumer/payment/create/entity")
    public CommonResult createEntity(Payment payment) {
        return restTemplate.postForEntity(PAYMENT_SERVICE + "/payment/create", payment, CommonResult.class).getBody();
    }

    @GetMapping("/consumer/payment/get/entity/{id}")
    public CommonResult getPaymentEntity(@PathVariable("id") Long id) {
        return restTemplate.getForEntity(PAYMENT_SERVICE + "/payment/get/" + id, CommonResult.class).getBody();
    }

    @GetMapping("/consumer/payment/lb")
    public String myLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if(instances==null |instances.size()==0){
            return null;
        }
        ServiceInstance instance = loadBalance.instances(instances);
        URI uri = instance.getUri();
        System.out.println(uri);
        return restTemplate.getForObject(uri +"/payment/lb",String.class);
    }

    @GetMapping("/consumer/payment/t")
    public String t(){
        return restTemplate.getForObject("http://192.168.120.103:8002/payment/lb",String.class);
    }

    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if(instances==null |instances.size()==0){
            return null;
        }
        ServiceInstance instance = loadBalance.instances(instances);
        URI uri = instance.getUri();
        System.out.println(uri);
        return restTemplate.getForObject(uri+"/payment/zipkin",String.class);
    }

}
